<script type="text/javascript">
    function countryChanged(comboBox) {
        var myForm = $(comboBox).closest('form').get(0);
        myForm.action = myForm.action.replace(/next$/, 'savePaymentForm');
        myForm.submit();
    }
</script>

<?php
echo form::open('user/registrationWizard/next', array('id'=>'wizardForm'));

echo '<div style="min-height: 120px">';

echo '<ul class="paymentPlaceList">';

if ($organizationList) {
	echo '<li>';
	echo '<label style="font-weight: bold">' . form::radioButton('paymentPlace', 'local', $paymentForm, 'onchange="togglePaymentPlace(this)"') . text::getEscaped('registration.paymentInCountryOfResidence') . '</label>';
	echo '<div class="' . ($paymentForm['payment_type'] != 'onArrival' ? '' : 'hidden') . '">';
	
	/*
	echo '<div>';
	echo '<label>' . text::getEscaped('registrationWizard.paymentCountry') . ':';
	echo '<select name="country_code" onchange="countryChanged(this)">';
	echo '<option></option>';
	foreach ($countryOptions as $countryCode => $countryName) {
	    echo '<option value="' . $countryCode . '" ';
	    if ($paymentForm['country_code'] == $countryCode) {
	        echo 'selected="selected"';
	    }
	    echo '>' . $countryName . '</option>';
	}
	echo '</select>';
	echo '</label>';
	echo '</div>';
	*/

    // Select an organization
    echo '<label><acronym title="' . text::getEscaped('registrationWizard-paymentOrganization-hint') . '">' . text::getEscaped('registrationWizard-paymentOrganization') . ':</acronym>' .
            form::comboBox('organization_id', $organizationOptions, $paymentForm, 'onchange="showPaymentTypesOfOrganization(this)"') .
            '</label>';
    
    echo '<div style="min-height: 70px;">';
    foreach ($organizationList as $organization) {
        echo '<div class="organizationPaymentOptions ' . ($paymentForm['organization_id'] == $organization['id'] ? '' : 'hidden') . '" id="organization_' . $organization['id'] . '">';
        // echo '<label>' . form::selectOneRadio('organization_id', $organization['id'], $paymentForm) . $organization['name'] . '</label><br/>';
        // Deadline style class to visualy indicate how far is the deadline - green/orange/red.
        $deadlineStyleClass = "paymentDeadline-far";
        $deadlineDistance = date::diffDates(sql::$TODAY, $organization['deadline']);
        if ($deadlineDistance == 0) {
            $deadlineStyleClass = "paymentDeadline-today";
        }
        else if ($deadlineDistance < 5) {
            $deadlineStyleClass = "paymentDeadline-close";
        }
        
        echo '<p class="description">' . text::getEscaped('paymentAcceptance.deadline') . ': <span class="' . $deadlineStyleClass . '">' . format::date($organization['deadline']) . '</span></p>';
        echo '<ul style="padding-left: 1em;" class="radioButtonList">';
        if ($organization['accepts_cash']) {
            echo '<li>';
            $extra = 'onchange="showPaymentDatails(this); selectOrganization(this);"';
            echo '<label>' . form::selectOneRadio('payment_type', 'cash', $paymentForm, $extra) . text::get('registration.paymentType-cash') . '</label>';
            $class = $paymentForm['payment_type'] != 'cash' ? 'hidden' : '';
            echo '<div class="paymentTypeDetails formField ' . $class . '" style="padding-left: 1em;"><label>' . text::getEscaped('paymentAcceptance.cash_notes') . '</label>' .
                    format::longText($organization['cash_notes']) . '</div>';
            echo '</li>';
        }
        if ($organization['accepts_bank_transfer']) {
            echo '<li>';
            $extra = 'onchange="showPaymentDatails(this); selectOrganization(this);"';
            echo '<label>' . form::selectOneRadio('payment_type', 'bank', $paymentForm, $extra) . text::get('registration.paymentType-bank') . '</label>';
            $class = $paymentForm['payment_type'] != 'bank' ? 'hidden' : '';
            echo '<div class="paymentTypeDetails ' . $class . '" style="padding-left: 1em;">';
            if ($organization['bank_account']) {
                $accountProperties = array('name', 'bank_name', 'number', 'bank_number', 'iban', 'swift');
                echo '<table class="displayTable">';
                echo '<tbody>';
                foreach ($accountProperties as $property) {
                    if ($organization['bank_account'][$property]) {
                        echo '<tr>';
                        echo '<th><label>' . text::getEscaped('bankAccount.' . $property) . '</label></th>';
                        echo '<td>' . $organization['bank_account'][$property] . '</td>';
                        echo '</tr>';
                    }
                }
                echo '<tr><th><label>' . text::getEscaped('paymentAcceptance.variable_symbol') . '</label></th><td>' . $organization['variable_symbol'] . '</td></tr>';
                echo '</tbody>';
                echo '</table>';
            }
            if ($organization['bank_transfer_notes']) {
                echo '<div class="formField"><label>' . text::getEscaped('paymentAcceptance.bank_transfer_notes') . '</label>' . format::longText($organization['bank_transfer_notes']) . '</div>';
            }
            echo '</div>';
            echo '</li>';
        }
        echo '</ul>';
        echo '</div>';
    }
    echo '</div>';
    echo '</div>';
    echo '</li>';
}

if ($this->getAuthUser('is_admin') || $_SERVER['REMOTE_ADDR'] == Kohana::config('yosemin.ashram_ip_address')) {
	echo '<li>';
	echo '<label>' . form::radioButton('paymentPlace', 'onArrival', $paymentForm, 'onchange="togglePaymentPlace(this)"') . text::getEscaped('registration.paymentOnArrival') . '</label>';
	echo '<div class="' . ($paymentForm['payment_type'] == 'onArrival' ? '' : 'hidden') . '"><p class="description">' . text::getEscaped('registrationWizard.paymentOnArrival-desc') . '</p>';
	if ($this->data->paymentOnArrivalFee) {
	    echo '<p class="description">' . html::img('notes/warningNote.png') . ' ' . text::getEscaped('registrationWizard.paymentOnArrival-warning', $this->data->paymentOnArrivalFee . ' ' . text::get('currency.mark-' . $this->data->priceGroup['currency_code'])) . '</p></div>';
	}
	echo '</li>';
}

echo '</ul>';

echo '</div>';

if ($unavailableOrganizationList) {
	echo '<div class="infoNote" style="font-size: small">';
	echo '<p>' . text::getEscaped('registrationWizard-unavailableOrgs') . ':</p>';
	echo '<ul>';
	foreach ($unavailableOrganizationList as $organization) {
		echo '<li>' . $organization['name'] . '</li>';
	}
	echo '</ul>';
	echo '</div>';
}

echo '<h2>' . text::getEscaped('registration.paymentNotes-header') . '</h2>';
echo '<p class="description">' . text::getEscaped('registration.paymentNotes-help') . '</p>';
echo form::textarea('payment_notes', $paymentForm['payment_notes']);

echo form::close();
?>

<script type="text/javascript">
$(document).ready(function () {
	// Automaticly select payment in a local organization it there is no other option. 
	var $radioButtons = $('input[name="paymentPlace"]');
	if ($radioButtons.size() == 1) {
		$radioButtons.get(0).checked = true;
	}
});

</script>